/*
 * Copyright © 2018 CODESTD.COM Inc. All rights reserved.
 */
package com.codestd.security.controller;

import com.codestd.security.service.AsyncService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

/**
 * 主控制器
 *
 * @author jaune
 * @since 1.0.0
 */
@Controller
public class MainController {

    private static Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private AsyncService asyncService;

    @GetMapping("/")
    public ModelAndView home() {
        return new ModelAndView("index");
    }

    @ResponseBody
    @GetMapping("/anonymous")
    public String main() {
        return "任何人都可以访问我";
    }

    @ResponseBody
    @GetMapping("/authc")
    public String authc() {

        // logger.info("Controller Thread:" + Thread.currentThread().getName());
        // logger.info("Controller principal: " + SecurityUtils.getSubject().getPrincipal().toString());

        // this.asyncService.testGetSubject();
        // this.asyncService.newThreadTest();
        Subject subject = SecurityUtils.getSubject();
        subject.getPrincipal();
        subject.checkRole("admin");

        SecurityUtils.getSecurityManager().checkRole(subject.getPrincipals(), "admin");

        return "需要登录才能访问";
    }

    @ResponseBody
    @GetMapping("/role")
    public String role() {
        return "需要有Admin角色才能访问";
    }

    @ResponseBody
    @GetMapping("/permissive")
    public String permissive() {
        return "permissive mode";
    }
}
